package com.elm.mapper;

import com.elm.po.Orders;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrdersMapper {
	@Insert("INSERT INTO orders (id, order_state, order_total, business_id,customer_id,address_id,status) " +
	            "VALUES (#{id}, #{orderState}, #{orderTotal}, #{businessId},#{customerId},#{addressId},#{status})")        //total的cart连携没做
	@Options(useGeneratedKeys = true, keyProperty = "id")
    int createOrders(Orders order);
    
	@Select("SELECT " +
	        "o.*, " +
	        "b.id AS b_id, b.business_name AS b_name, b.business_img AS b_img, " +
	        "b.business_address AS b_addr, b.business_explain AS b_explain, " +
	        "b.start_price AS b_startPrice, b.delivery_price AS b_deliveryPrice, " +
	        "d.id AS d_id, d.contact_name AS d_name, d.contact_sex AS d_sex, " +
	        "d.contact_tel AS d_tel, d.address AS d_addr, d.is_default AS d_default " +
	        "FROM orders o " +
	        "LEFT JOIN business b ON o.business_id = b.id " +
	        "LEFT JOIN delivery_address d ON o.address_id = d.id " +
	        "WHERE o.id = #{id}")
	@Results({
	    // 映射Orders自身字段
	    @Result(column = "id", property = "id"),
	    @Result(column = "order_state", property = "orderState"),
	    @Result(column = "order_total", property = "orderTotal"),
	    @Result(column = "business_id", property = "businessId"),
	    @Result(column = "customer_id", property = "customerId"),
	    @Result(column = "address_id", property = "addressId"),
	    @Result(column = "status", property = "status"),
	    @Result(column = "comment", property = "comment"),
	    @Result(column = "star", property = "star"),
	    @Result(column = "review", property = "review"),
	    
	    // 映射关联的Business对象
	    @Result(column = "b_id", property = "business.id"),
	    @Result(column = "b_name", property = "business.businessName"),
	    @Result(column = "b_img", property = "business.businessImg"),
	    @Result(column = "b_addr", property = "business.businessAddress"),
	    @Result(column = "b_explain", property = "business.businessExplain"),
	    @Result(column = "b_startPrice", property = "business.startPrice"),
	    @Result(column = "b_deliveryPrice", property = "business.deliveryPrice"),
	    
	    // 映射关联的DeliveryAddress对象
	    @Result(column = "d_id", property = "deliveryAddress.id"),
	    @Result(column = "d_name", property = "deliveryAddress.contactName"),
	    @Result(column = "d_sex", property = "deliveryAddress.contactSex"),
	    @Result(column = "d_tel", property = "deliveryAddress.contactTel"),
	    @Result(column = "d_addr", property = "deliveryAddress.address"),
	    @Result(column = "d_default", property = "deliveryAddress.isDefault"),
	    
	    // 映射关联的OrderDetailet列表（通过子查询关联）
	    @Result(column = "id", property = "orderdetailetList", 
	            many = @Many(select = "com.elm.mapper.OrderDetailetMapper.getDetailetsByOrder"))
	})
	Orders getOrdersById(Integer id);
    
    @Select("select * from orders where customer_id=#{userId}")
    List<Orders> getOrdersByUserId(int userId);
    
    @Update("update orders set comment=#{comment},star=#{star},review=0 where id=#{id}")
    int addComment(Orders order);
    
    @Select("select * from orders where business_id=#{businessId} And comment!='' ")
    List<Orders> getAllCommentsByBusinessId(Integer businessId);
    
    @Select("select * from orders where comment!='' And review=0")
    List<Orders> getAllComments();
    
    @Update("update orders set comment=null,star=null,review=1 where id=#{id}")
    int deleteComment(Integer orderId);
     
    @Select("select * from orders where business_id=#{businessId}")
    List<Orders> getAllOrders(Integer businessId);
    
    @Select("select * from orders where business_id=#{businessId} And status='paid'")
    List<Orders> getAllPaidOrders(Integer businessId);
    
    @Select("select * from orders where business_id=#{businessId} And status='ended'")
    List<Orders> getAllEndedOrders(Integer businessId);
    
    @Update("update orders set status=#{orderState} where id=#{orderId}")
    int handleOrders( Integer orderId, String orderState);

    @Update("update orders set review=1 where id=#{orderId}")
	Integer reviewComment(Integer orderId);
   
} 